<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>二分查找</title>
</head>

<body>
    <script>
        // 时间复杂度 O（log n）
        function dicSearch(orderedArr, target) {
            //两个函数是防止递归时数据初始化，将初始化放在子函数外
            let left = 0;
            let right = orderedArr.length - 1;
            let min = Math.round((left + right) / 2);
            while (left <= right) {
                //当条件满足时一直循环
                if (orderedArr[min] !== target) {
                    if (orderedArr[min] < target) {
                        //先将等与不等划分，在判断大小
                        left = min + 1;
                        min = Math.round((left + right) / 2);
                        console.log(min);
                    } else {
                        right = min - 1;
                        min = Math.round((left + right) / 2);
                        console.log(min);
                    }
                } else {
                    console.log(min);
                    return 1;
                }
            }
            console.log("未找到");
        }
        dicSearch([1, 2, 3, 4, 6, 7, 7, 8, 9, 15, 17, 27], 17);
    </script>
</body>

</html>